<script>
import { unique } from '@/utils/string'

export default {
  name: 'UiChartSeries',
  inject: {
    UiChart: {
      from: 'UiChart'
    },
    UiChartType: {
      from: 'UiChartType',
      default: null
    }
  },
  props: {
    data: {
      type: Array
    },
    type: {
      type: String
    },
    name: {
      type: String
    }
  },
  created() {
    if (this.UiChartType) {
      if (this.type) {
        throw new Error('[UiChart]已经指定图表类型，无需配置type')
      }
    } else if (!this.type) {
      throw new Error('[UiChart]缺少type配置')
    }
    this.id = unique()
    this.UiChart.addSeries({
      id: this.id,
      name: this.name,
      type: this.UiChartType || this.type,
      data: this.data
    })
  },
  render() {
    return null
  },
  beforeDestroy() {
    this.UiChart.removeSeries(this.id)
  }
}
</script>
